package com.qn.wechat.service;

import com.jeesuite.common.json.JsonUtils;
import com.jeesuite.confcenter.ConfigChangeHanlder;
import com.jeesuite.mybatis.plugin.pagination.Page;
import com.jeesuite.spring.ApplicationStartedListener;
import com.jeesuite.springweb.model.WrapperResponse;
import com.oneplatform.base.exception.AssertUtil;
import com.qn.wechat.common.MapperUtils;
import com.qn.wechat.dao.entity.SBPromoteEntity;
import com.qn.wechat.dao.entity.ext.SBPromoteEntityExt;
import com.qn.wechat.dao.mapper.SBPromoteEntityMapper;
import com.qn.wechat.ifservice.dto.PrmDescDto;
import com.qn.wechat.ifservice.dto.SBPromoteDto;
import com.qn.wechat.ifservice.dto.para.SBPromoteQeuryPara;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SBPromoteService implements ConfigChangeHanlder, ApplicationStartedListener {

    @Resource
    SBPromoteEntityMapper sbPromoteEntityMapper;
    @Resource
    IdService idService;

    @Resource
    MapperUtils mapperUtils;

    public Page<SBPromoteDto> getList(@RequestBody SBPromoteQeuryPara para) {

        Page<SBPromoteDto> response = new Page<SBPromoteDto>();

        Page<SBPromoteEntityExt> list = sbPromoteEntityMapper.findSbPromote(para, para);

        List<SBPromoteDto> dtos = null;
        if (list != null && list.getData() != null)
            dtos = mapperUtils.map(list.getData(), SBPromoteDto.class);

        if (list != null) {
            BeanUtils.copyProperties(list, response);
            for (int i = 0; i < list.getData().size(); i++) {
                String desc = list.getData().get(i).getPrmdesc();
                PrmDescDto prmDescDto = null;
                try {
                    prmDescDto = JsonUtils.toObject(desc, PrmDescDto.class);
                } catch (RuntimeException ex) {

                }
                if (prmDescDto != null)
                    dtos.get(i).setPrmdescModel(prmDescDto);

            }
        }
        response.setData(dtos);
        return response;
    }


    public WrapperResponse<String> save(SBPromoteDto dto) {

        WrapperResponse<String> response = new WrapperResponse<>();
        if (dto.getPrmdescModel() == null || dto.getPrmdescModel().getBookNames() == null || dto.getPrmdescModel().getBookNames().size() == 0) {
            AssertUtil.isNull(null, "活动赠送内容不能为空");
        }

        SBPromoteEntity entity = new SBPromoteEntity();
        BeanUtils.copyProperties(dto, entity);

        entity.setPrmdesc(JsonUtils.toJson(dto.getPrmdescModel()));

        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(String.valueOf(idService.genId()));
            entity.setAlreadycount(0);
            entity.setCreatetime(new Date());
            sbPromoteEntityMapper.insert(entity);
        } else {
            sbPromoteEntityMapper.updateByPrimaryKeySelective(entity);
        }

        response.setData(entity.getId());
        return response;
    }

    @Override
    public void onConfigChanged(Map<String, Object> map) {

    }

    @Override
    public void onApplicationStarted(ApplicationContext applicationContext) {

    }
}
